home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol05 / 05 / wintro2 / stock.c < prev    next >
Text File  |  1990-09-01  |  8KB  |  319 lines

  1. /*
  2.   Microsoft Systems Journal Stock Application
  3.  
  4.   Written by Marc Adler
  5.              Magma Systems
  6.              15 Bodwell Terrace
  7.              Millburn, New Jersey  07041
  8.  
  9.   This code accompanies PartII of MSJ's series on learning Windows.
  10.   It is the basic skeleton of our  application. The main thing we
  11.   do here is perform the initialization, and print out help messages
  12.   for each menu item.
  13. */
  14.  
  15. #include "windows.h"
  16. #include "stock.h"
  17.  
  18. /*
  19.   Global Variables
  20. */
  21. HANDLE hThisInstance;         /* Program instance handle */
  22. HWND   hWndMain;              /* Handle to main window   */
  23. HWND   hWndStatus;            /* Window for status mesages */
  24. HANDLE hAccelTable;           /* Handle to the accelerator table */
  25.  
  26.  
  27. int NEAR PASCAL WinMain(hInstance, hPrevInstance,
  28.                         lpszCmdLine, nCmdShow)
  29.   HANDLE hInstance;
  30.   HANDLE hPrevInstance;
  31.   LPSTR  lpszCmdLine;
  32.   int    nCmdShow;
  33. {
  34.   MSG msg;
  35.  
  36.   /*
  37.     Save the handle to this inctance.
  38.   */
  39.   hThisInstance = hInstance;
  40.  
  41.   /*
  42.     If this is the first instance of the app,
  43.     register window classes...
  44.   */
  45.   if (!hPrevInstance)
  46.   {
  47.     if (!InitializeApplication())
  48.     {
  49.       MessageBeep(0);
  50.       return FALSE;
  51.     }
  52.   }
  53.  
  54.   /*
  55.     Perform the initialization for this particular instance...
  56.   */
  57.   if (!InitializeInstance(lpszCmdLine, nCmdShow))
  58.     return FALSE;
  59.  
  60.  
  61.   /*
  62.     The main message loop...
  63.   */
  64.   while (GetMessage(&msg, (HWND) NULL, 0, 0))
  65.   {
  66.     if (!TranslateAccelerator(hWndMain, hAccelTable, &msg))
  67.     {
  68.       TranslateMessage(&msg);
  69.       DispatchMessage(&msg);
  70.     }
  71.   }
  72.  
  73.   return 0;
  74. }
  75.  
  76.  
  77. BOOL FAR PASCAL InitializeApplication(VOID)
  78. {
  79.   WNDCLASS wc;
  80.  
  81.   /*
  82.     Register the main window class
  83.   */
  84.   wc.style         = CS_VREDRAW | CS_HREDRAW;
  85.   wc.lpfnWndProc   = MainWndProc;
  86.   wc.cbClsExtra    = 0;
  87.   wc.cbWndExtra    = 0;
  88.   wc.hInstance     = hThisInstance;
  89.   wc.hIcon         = LoadIcon(hThisInstance,
  90.                      MAKEINTRESOURCE(ID_STOCK));
  91.   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  92.   wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  93.   wc.lpszMenuName  = "StockMenu";
  94.   wc.lpszClassName = "StockMainWindow";
  95.   if (!RegisterClass(&wc))
  96.     return FALSE;
  97.  
  98.   wc.style         = CS_VREDRAW | CS_HREDRAW;
  99.   wc.lpfnWndProc   = StatusWndProc;
  100.   wc.cbClsExtra    = 0;
  101.   wc.cbWndExtra    = 0;
  102.   wc.hInstance     = hThisInstance;
  103.   wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  104.   wc.lpszMenuName  = NULL;
  105.   wc.lpszClassName = "StockStatus";
  106.   if (!RegisterClass(&wc))
  107.     return FALSE;
  108.  
  109.   return TRUE;
  110. }
  111.  
  112.  
  113. BOOL FAR PASCAL InitializeInstance(LPSTR lpCmdLine, WORD nCmdShow)
  114. {
  115.   /*
  116.     Create the main window
  117.   */
  118.   hWndMain = CreateWindow("StockMainWindow",
  119.                           "MSJ Stock Application",
  120.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  121.                           CW_USEDEFAULT, CW_USEDEFAULT,
  122.                           CW_USEDEFAULT, CW_USEDEFAULT,
  123.                           (HWND) NULL,
  124.                           (HMENU) NULL,
  125.                           hThisInstance,
  126.                           (LPSTR) NULL);
  127.   if (!hWndMain)
  128.     return FALSE;
  129.  
  130.   /*
  131.     Load the accelerators in
  132.   */
  133.   hAccelTable = LoadAccelerators(hThisInstance,
  134.                                  (LPSTR) "StockAccelerators");
  135.  
  136.   /*
  137.     Display the main window
  138.   */
  139.   ShowWindow(hWndMain, nCmdShow);
  140.   UpdateWindow(hWndMain);
  141.  
  142.   return TRUE;
  143. }
  144.  
  145.  
  146. LONG FAR PASCAL MainWndProc(hWnd, msg, wParam, lParam)
  147.   HWND hWnd;
  148.   WORD msg;
  149.   WORD wParam;
  150.   LONG lParam;
  151. {
  152.   char szMsg[80];
  153.   RECT rc;
  154.   int  i;
  155.  
  156.   static HWND ahwndSubMenus[16];
  157.  
  158.  
  159.   switch (msg)
  160.   {
  161.     case WM_CREATE  :
  162.       /*
  163.         Create a status window at the bottom of the main window.
  164.       */
  165.       GetClientRect(hWnd, (LPRECT) &rc);
  166.       hWndStatus = CreateWindow("StockStatus",
  167.                                  NULL,
  168.                                  WS_CHILD | WS_BORDER,
  169.                                  rc.left, rc.bottom - 20,
  170.                                  rc.right - rc.left, 20,
  171.                                  hWnd,
  172.                                  NULL,
  173.                                  hThisInstance,
  174.                                  (LPSTR) 0L);
  175.       if (hWndStatus)
  176.         ShowWindow(hWndStatus, SW_SHOW);
  177.  
  178.       /*
  179.         We gather up the window handles of all of the sub menus.
  180.       */
  181.       for (i=0; (ahwndSubMenus[i]=GetSubMenu(GetMenu(hWnd),i)); i++)
  182.         ;
  183.  
  184.       break;
  185.  
  186.  
  187.     case WM_COMMAND :
  188.       switch (wParam)
  189.       {
  190.         case ID_NEW             :
  191.           break;
  192.         case ID_OPEN            :
  193.           break;
  194.         case ID_SAVE            :
  195.           break;
  196.         case ID_CLOSE           :
  197.           break;
  198.         case ID_PRINT           :
  199.           break;
  200.         case ID_TICK_ADD        :
  201.           break;
  202.         case ID_TICK_CHANGE     :
  203.           break;
  204.         case ID_GRAPH_PRICE     :
  205.           break;
  206.         case ID_GRAPH_VOLUME    :
  207.           break;
  208.         case ID_GRAPH_ZOOM      :
  209.           break;
  210.         case ID_GRAPH_COLORS    :
  211.           break;
  212.         case ID_GRAPH_GRID_HORZ :
  213.           break;
  214.         case ID_GRAPH_GRID_VERT :
  215.           break;
  216.         case ID_ABOUT           :
  217.           break;
  218.         case ID_EXIT            :
  219.           PostQuitMessage(0);
  220.           break;
  221.       }
  222.       break;
  223.  
  224.     case WM_MENUSELECT          :
  225.     {
  226.       WORD idMenu;
  227.       WORD iLen;
  228.  
  229.       /*
  230.         We trigger off of the WM_MENUSELECT message so that we
  231.         can print informative messages about each menu option.
  232.       */
  233.  
  234.       if (lParam & MF_POPUP)
  235.       {
  236.         /*
  237.           The handle of the popup menu is returned in wParam. Search
  238.           the array of sub-menu window handles in order to see which
  239.           submenu we are in.
  240.         */
  241.         for (idMenu = 0;
  242.              ahwndSubMenus[idMenu] && wParam !=
  243.                                               ahwndSubMenus[idMenu];
  244.              idMenu++)
  245.           ;
  246.         if (!ahwndSubMenus[idMenu])
  247.           break;
  248.         idMenu++;  /* make it a 1-offset value */
  249.       }
  250.       else
  251.         /*
  252.           An actual menu item identifier is returned in wparam...
  253.         */
  254.         idMenu = wParam;
  255.  
  256.       /*
  257.         If lParam was <-1,0>, then the user dismissed the menu, so
  258.         we just want to erase the previous help text from the
  259.         status window. If not, then idMenu contains a valid menu or
  260.         submenu identifier. Load the corresponding string from
  261.         the string table, and print it.
  262.       */
  263.       if (LOWORD(lParam) == -1 && HIWORD(lParam) == 0  ||
  264.          ((iLen = LoadString(hThisInstance, idMenu, (LPSTR) szMsg,
  265.                              sizeof(szMsg)))))
  266.       {
  267.         HDC hDC = GetDC(hWndStatus);
  268.         HBRUSH hBrush, hOldBrush;
  269.  
  270.         /*
  271.           Use a white brush for erasing.
  272.         */
  273.         hBrush = CreateSolidBrush(0x00FFFFFF);
  274.         hOldBrush = SelectObject(hDC, hBrush);
  275.  
  276.         /*
  277.           Erase the previous menu help text, if any. Then display the
  278.           new help text.
  279.         */
  280.         GetClientRect(hWndStatus, (LPRECT) &rc);
  281.         FillRect(hDC, &rc, hBrush);
  282.         if (LOWORD(lParam) == -1 && HIWORD(lParam) == 0)
  283.           ;
  284.         else
  285.           TextOut(hDC, 0, 0, szMsg, iLen);
  286.  
  287.         /*
  288.           Restore the old brush.
  289.         */
  290.         SelectObject(hDC, hOldBrush);
  291.         DeleteObject(hBrush);
  292.         ReleaseDC(hWndStatus, hDC);
  293.       }
  294.       break;
  295.     }
  296.  
  297.  
  298.     case WM_DESTROY :
  299.       PostQuitMessage(0);
  300.       break;
  301.  
  302.     default :
  303.       return DefWindowProc(hWnd, msg, wParam, lParam);
  304.   }
  305.  
  306.   return (LONG) TRUE;
  307. }
  308.  
  309.  
  310.  
  311. LONG FAR PASCAL StatusWndProc(hWnd, msg, wParam, lParam)
  312.   HWND hWnd;
  313.   WORD msg;
  314.   WORD wParam;
  315.   LONG lParam;
  316. {
  317.   return DefWindowProc(hWnd, msg, wParam, lParam);
  318. }
  319.